package net.yla.smartbus.core.util.data;

import net.yla.smartbus.core.exception.AppException;

import java.util.Set;

/**
 * @author Handy Wong
 * @date 2020/8/24 14:03
 */
public class DataTypeUtil {


    /**
     * 32Int cast to byte[]
     *
     * @param number
     * @return
     */
    public static byte[] getByteBy32Int(int number) {
        //0xff表示的数二进制1111 1111 占一个字节.和其进行&操作的数,最低8位,不会发生变化
        byte[] buffer = new byte[4];
        buffer[0] = (byte) ((number >> 24) & 0xff);
        buffer[1] = (byte) ((number >> 16) & 0xff);
        buffer[2] = (byte) ((number >> 8) & 0xff);
        buffer[3] = (byte) (number & 0xff);
        return buffer;
    }

    public static int get32IntByByte(byte[] buffer) {
        int temp = 0;
        for (int i = 0; i < 4; i++) {
            temp |= ((buffer[i] & 0xff) << ((3 - i) * 8));
        }
        return temp;
    }

    /**
     * @param buffer
     * @param offset
     * @return
     */
    public static int get32IntByByte(byte[] buffer, int offset) {
        int temp = 0;
        for (int i = offset, j = 0; i < (offset + 4); i++, j++) {
            temp |= ((buffer[i] & 0xff) << ((3 - j) * 8));
        }
        return temp;
    }

    /**
     * 从byte[]指定位置起将32位整形数转换为byte并放入byte[]
     *
     * @param value
     * @param offset
     * @param buffer
     */
    public static void setBytes(int value, int offset, byte[] buffer) {
        buffer[offset] = (byte) ((value >> 24) & 0xff);
        buffer[offset + 1] = (byte) ((value >> 16) & 0xff);
        buffer[offset + 2] = (byte) ((value >> 8) & 0xff);
        buffer[offset + 3] = (byte) (value & 0xff);
    }

    /**
     * byte[] cast to int[]
     *
     * @param buffer
     * @return
     */
    public static int[] getIntArray(byte[] buffer) {
        if (buffer == null) {
            return null;
        }
        int remainder = buffer.length % 4;
        if (remainder != 0) {
            throw new AppException("Sorry,This will be converted byte's array could not cast to a new array of int,Maybe the length of be converted byte's array is not right!", 500);
        }
        int[] result = new int[buffer.length / 4];
        for (int i = 0, j = 0; i < buffer.length; i += 4, j++) {
            result[j] = get32IntByByte(buffer, i);
        }
        return result;
    }

    /**
     * set cast to int[]
     *
     * @param allSet
     * @return
     */
    public static int[] SetToInt(Set<Integer> allSet) {
        // 先将set集合转为Integer型数组
        Integer[] temp = allSet.toArray(new Integer[]{});//关键语句

        // 再将Integer型数组转为int型数组
        int[] intArray = new int[temp.length];
        for (int i = 0; i < temp.length; i++) {
            intArray[i] = temp[i].intValue();
        }
        return intArray;
    }

    /**
     * int[] cast to byte[]
     *
     * @param values
     * @return
     */
    public static byte[] getByteArray(int[] values) {
        if (values == null) {
            return null;
        }
        byte[] result = new byte[values.length * 4];
        for (int i = 0; i < values.length; i++) {
            setBytes(values[i], i * 4, result);
        }
        return result;
    }


    public static byte[] getByteArray(Integer[] values) {
        byte[] result = new byte[values.length * 4];
        for (int i = 0; i < values.length; i++) {
            setBytes(values[i], i * 4, result);
        }
        return result;
    }

    public static void main(String[] args) {
//        byte[] byteBy32Int = DataTypeUtil.getByteBy32Int(191);
//
//        int c=get32IntByByte(byteBy32Int);
//        System.out.println(c);


        int[] a = {123, 5654, -4646, 454545, 0};
        byte[] buffer = getByteArray(a);
        for (byte b : buffer) {
            //  System.out.println(b);
        }
        int[] d = getIntArray(buffer);
        for (int k : d) {
            System.out.println(k);
        }

    }


}
